package com.memory.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.monitor.annotation.AuthCheck;
import com.memory.monitor.common.BaseResponse;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ResultUtils;
import com.memory.monitor.constant.UserConstant;
import com.memory.monitor.model.dto.institution.InstitutionAddRequest;
import com.memory.monitor.model.dto.institution.InstitutionQueryRequest;
import com.memory.monitor.model.dto.institution.InstitutionUpdateRequest;
import com.memory.monitor.model.entity.Institution;
import com.memory.monitor.service.InstitutionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户接口
 *
 * @author <a href="http://deng-2022.atomgit.net/blog/">Memory</a>
 * @from <a href="https://gitee.com/deng-2022">deng-2022</a>
 */
@RestController
@RequestMapping("/institution")
@Slf4j
public class InstitutionController {
    @Resource
    private InstitutionService institutionService;

    /**
     * 分页获取信息
     *
     * @param institutionQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Institution>> listByPageInstitution(@RequestBody InstitutionQueryRequest institutionQueryRequest) {
        long current = institutionQueryRequest.getCurrent();
        long size = institutionQueryRequest.getPageSize();
        // Page<User> userPage = userService.page(new Page<>(current, size),
        //         userService.getQueryWrapper(userQueryRequest));
        Page<Institution> userPage = institutionService.page(new Page<>(current, size));

        return ResultUtils.success(userPage);
    }

    /**
     * 增加信息 返回id
     *
     * @param institutionAddRequest

     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addInstitution(@RequestBody InstitutionAddRequest institutionAddRequest){
        Long id = institutionService.addInstitution(institutionAddRequest);
        return ResultUtils.success(id);
    }

    /**
     * 修改信息 返回boolean
     *
     * @param institutionUpdateRequest

     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateInstitution(@RequestBody InstitutionUpdateRequest institutionUpdateRequest){
        Boolean result= institutionService.updateInstitution(institutionUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除信息 返回boolean
     *
     * @param deleteRequest

     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInstitution(@RequestBody DeleteRequest deleteRequest){
        Boolean result = institutionService.deleteInstitution(deleteRequest);
        return ResultUtils.success(result);
    }

}
